﻿var uploadImgCutInit = function (id) {
    //添加的文件数量
    var fileCount = 0;
    //上传成功的文件数量 
    var fileSuccess = 0;

    //浏览器兼容性识别
    if (!WebUploader.Uploader.support()) {
        manage.msg("不支持您的浏览器！如果你使用的是IE浏览器，请尝试升级 flash 播放器");
        throw new Error('WebUploader does not support the browser you are using.');
    }

    //上传组件初始化
    var uploader = WebUploader.create({
        pick: {
            id: ".thumbtextbtn",
            multiple: false
        },
        auto: true,
        accept: {
            title: 'Images',
            extensions: 'gif,jpg,jpeg,bmp,png',
            mimeTypes: 'image/jpg,image/jpeg,image/png'
        },
        swf: "/content/utility/webuploader/uploader.swf",
        server: "/other/common/uploadcut",
        fileSingleSizeLimit: 10 * 1024 * 1024    // 10 M
    });

    var button = undefined, fileList = undefined, imgDir = $("#img_dir").val();

    $(".thumbtextbtn").click(function () {
        button = $(this);
        fileList = button.next(".thumbfilelist");
    });

    //添加文件列队
    uploader.onFileQueued = function (file) {
        var item = $("<div id='" + file.id + "' class='uploadItem'>"
            + "<div class='title'>" + file.name + "</div>"
            + "<div class='progress'><span></span></div></div>");

        fileCount++;
        item.appendTo(fileList);
    };

    //开始上传
    uploader.onStartUpload = function () {
        //上传之前传递数据
        uploader.options.formData.imgDir = imgDir;
        uploader.options.formData.width = $("#img_width").val();
        uploader.options.formData.height = $("#img_height").val();

        fileList.show();
    };

    //进度条更新
    uploader.onUploadProgress = function (file, percentage) {
        var item = $("#" + file.id);
        var percent = item.find(".progress span");
        percent.css("width", percentage * 100 + "%");
    };

    //文件上传错误
    uploader.onUploadError = function (file, reason) {
        var item = $("#" + file.id);
        var percent = item.find(".progress span");
        percent.css("background", "#ff0000");
    };

    //文件上传完毕
    uploader.onUploadSuccess = function (file, response) {
        var item = $("#" + file.id);
        var percent = item.find(".progress span");
        percent.css("background", "#44b549");

        fileSuccess++;

        button.prevAll("img").attr("src", "/userfiles" + imgDir + response.file);
        $("#img_file").val(response.file);
    };

    //所有文件上传完毕
    uploader.onUploadFinished = function () {
        if (fileCount == fileSuccess && fileSuccess) {
            manage.slide("上传完毕！"); fileListClose();
        }
        else if (fileCount) {
            manage.confirm("文件上传失败，是否重试？", function (ok) {
                if (ok) { uploader.retry(); } else { fileListClose(); }
            });
        }
        else { fileListClose(); }
    };

    //所有事件
    uploader.on('all', function (type) {
        //console.info('all: ' + type);
    });

    //发生错误
    uploader.onError = function (code) {
        console.info('onError: ' + code);
        manage.msg("文件错误！");
    };

    var fileListClose = function () {
        var files = uploader.getFiles();
        if (files) {
            $(files).each(function (i, e) {
                uploader.removeFile(e, true);
            });
        }
        uploader.reset();
        fileCount = 0; fileSuccess = 0;
        fileList.empty(); fileList.hide();
    };

    return uploader;
};